package com.socialuni.common.user.sdk.manage;

import com.socialuni.common.user.model.DO.CommonTokenDO;
import com.socialuni.common.user.sdk.constant.DateTimeType;
import com.socialuni.common.user.sdk.repository.CommonTokenRepository;
import com.socialuni.common.user.sdk.utils.CommonTokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CommonUserTokenCreate {
    @Resource
    CommonTokenRepository uniTokenRepository;

    public CommonTokenDO create(Integer userId) {
        String token = CommonTokenUtil.generateTokenById(userId);
        CommonTokenDO socialUserTokenDO = this.create(userId, token);
        return socialUserTokenDO;
    }

    public CommonTokenDO create(Integer userId, String token) {
        CommonTokenDO socialUserTokenDO = new CommonTokenDO();
        socialUserTokenDO.setToken(token);
        socialUserTokenDO.setUserId(userId);
        Date curDate = new Date();
        socialUserTokenDO.setLoseTime(new Date(curDate.getTime() + DateTimeType.quarter));

        uniTokenRepository.save(socialUserTokenDO);
        return socialUserTokenDO;
    }
}
